// TerrainDemo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "Graphics/SystemEvents.h"
#include "Graphics/Input.h"
#include "Graphics/Direct3D.h"
#include "Graphics/Camera.h"

#include "Terrain/Terrain.h"



int _tmain(int argc, _TCHAR* argv[])
{
	GraphicDeviceD3D11* pDevice = new GraphicDeviceD3D11;


	if(pDevice->Initialize(1280, 720, false))
	{
		NavigationCamera	camera;
		Chronometer			chrono;


		ConstantBuffer<CameraInfo>*	pViewCB = new ConstantBuffer<CameraInfo>(pDevice->GetDevice());

		InputManager::Initialize();

		Terrain* pTerrain = new Terrain;
		pTerrain->Initialize(pDevice, "../terrain.raw", 64);

		chrono.Start();
		while(SystemEvents::Update())
		{
			camera.Update((float)chrono.GetElapsedTime());

			pViewCB->UpdateParams(camera.GetCameraInfo());

			ID3D11Buffer* pNull[] = {NULL, NULL, NULL};
			
			pDevice->GetImmediateContext()->PSSetConstantBuffers(0, 3, pNull);
			pDevice->GetImmediateContext()->VSSetConstantBuffers(0, 3, pNull);
			pDevice->GetImmediateContext()->GSSetConstantBuffers(0, 3, pNull);

			pViewCB->Upload(pDevice->GetImmediateContext());
			
			pDevice->GetImmediateContext()->PSSetConstantBuffers(0, 1, pViewCB->GeConstantBuffers());
			pDevice->GetImmediateContext()->VSSetConstantBuffers(0, 1, pViewCB->GeConstantBuffers());
			pDevice->GetImmediateContext()->GSSetConstantBuffers(0, 1, pViewCB->GeConstantBuffers());

			pTerrain->Render();

			pDevice->Present();
			//Sleep(1);
		}

		pTerrain->Finalize();
		_SAFE_DELETE(pTerrain);
		_SAFE_DELETE(pViewCB);

		InputManager::Finalize();
	}
	pDevice->Finalize();

	_SAFE_DELETE(pDevice);
	return 0;
}

